function TS = getYahooData(ticker, startDate, endDate, data_path, period)
% TS = getYahooData(ticker, startDate, endDate)
% returns a TS structure with Yahoo stock data including log returns
%
% example: 
%   TS = getYahooData('SPY', '1/1/2014')  % endDate defaults to today
%

if nargin < 5, period = 'd'; end
if nargin < 4, data_path = [getenv('wcm_root') 'data/yahoo_data/']; end
if nargin < 3, endDate = date; end
if nargin < 2, startDate = datenum('01/01/1950'); end

sd = datevec(startDate);
ed = datevec(endDate);

a = num2str(sd(2)-1,'%02d');
b = num2str(sd(3));
c = num2str(sd(1));
d = num2str(ed(2)-1,'%02d');
e = num2str(ed(3));
f = num2str(ed(1));
g = period;

yahoo = getHttpTSdata(strcat('http://ichart.finance.yahoo.com/table.csv?s=', ...
    ticker, '&a=', a, '&b=', b, '&c=', c, '&d=', d, '&e=', e, '&f=', f,'&g=', g,'&ignore=.csv'));

% reverse data so that oldest data is first in TS structure (yahoo puts oldest data last)
yahoo.dates = yahoo.dates(end:-1:1);
yahoo.data = yahoo.data(end:-1:1,:);
numdays = length(yahoo.dates);

if strcmp(period, 'v')
  TS = yahoo;
  writeTS(TS, data_path, strcat(ticker,'.csv'));
  %append data to the all_div file
  TS.header = {ticker};
  if TS.dates(1) > 999999
    TS = dlyTS2monthly(TS,'sum');
  end
  all_div = readTS(data_path, 'all_div.csv');
  if isstruct(all_div)
    if all_div.dates(1) > 999999
      all_div = dlyTS2monthly(all_div,'sum');
    end
    all_div = joinTS(all_div, TS, 'both', 'union');
  else
    all_div = TS;
  end
  all_div.data(isnan(all_div.data))=0;
  writeTS(all_div, data_path, 'all_div.csv')
  
else  
  adjclose = joinTS(yahoo, {'Adj Close'}, 'headers');
  ret = buildTS(zeros(size(yahoo.dates)), {'Log Returns'}, yahoo.dates);
  ret.data(2:end) = log(adjclose.data(2:end)./adjclose.data(1:end-1));

  TS = joinTS(yahoo, ret, 'headers','union');

  if length(ticker)>=3 && strcmp(ticker(1:3), '%5E')
    ticker = ticker(4:end);
  end
      
  writeTS(TS, data_path, strcat(ticker,'.csv'));

  %append data to the all_ret and all_price files
  all_ret = readTS(data_path, 'all_ret.csv');
  ret.header = {ticker};
  if isstruct(all_ret)
      all_ret = joinTS(all_ret, ret, 'both', 'union');
  else
      all_ret = ret;
   end
  writeTS(all_ret, data_path, 'all_ret.csv')

  all_price = readTS(data_path, 'all_price.csv');
  adjclose.header = {ticker};
  if isstruct(all_price)
      all_price = joinTS(all_price, adjclose, 'both', 'union');
  else
      all_price = adjclose;
  end
  writeTS(all_price, data_path, 'all_price.csv')
end
 
end